package com.example.uupaymuch.controller.health;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

@Component
public class HealthCheckApp {

    private static final Logger logger = Logger.getLogger(HealthCheckApp.class.getName());
    private Timer timer;

    @PostConstruct
    public void healthInit() {
        timer = new Timer();

        // 定义一个任务，每 5 秒执行一次
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                try {
                    logger.log(Level.INFO, "服务仍在正常运行");
                } catch (Exception e) {
                    logger.log(Level.SEVERE, "健康检查任务发生异常", e);
                }
            }
        };

        // 安排任务在 0 毫秒后开始，并每 5000 毫秒执行一次
        timer.scheduleAtFixedRate(task, 0, 5000);
    }

    @PreDestroy
    public void cleanUp() {
        if (timer != null) {
            timer.cancel(); // 取消定时任务
        }
    }
}